<xsl:stylesheet version="1.0"
  xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
  xmlns:xlink="http://www.w3.org/1999/xlink"
  xmlns:db="http://docbook.org/ns/docbook"
  xmlns:pbs="http://schema.pbs.gov.au"
  xmlns:rwt="http://schema.pbs.gov.au/RWT/"
  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">

  <db:article>
    <db:info>
      <db:title>Cost Efficient Vial Combination</db:title>
      <db:subtitle>Grouping and Ordering</db:subtitle>

      <db:copyright>
        <db:year>2013</db:year>
        <db:year>2012</db:year>
        <db:year>2011</db:year>
        <db:holder>Commonwealth of Australia</db:holder>
      </db:copyright>

      <db:legalnotice>
        <db:para>This software is copyrighted by the Commonwealth of Australia ("The Commonwealth").  The following terms apply to all files associated with the software unless explicitly disclaimed in individual files.</db:para>
        <db:para>Permission is hereby granted to use, copy, modify, distribute, and license this software and its documentation for any purpose, provided that existing copyright notices are retained in all copies and that this notice is included verbatim in any distributions. No written agreement, license, or royalty fee is required for any of the authorized uses. Modifications to this software may be copyrighted by their authors and need not follow the licensing terms described here, provided that the new terms are clearly indicated on the first page of each file where they apply.</db:para>
        <db:para>IN NO EVENT SHALL THE COMMONWEALTH OR DISTRIBUTORS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</db:para>
        <db:para>THE COMMONWEALTH SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT.  THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, AND THE COMMONWEALTH HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.</db:para>
      </db:legalnotice>

      <db:author>
        <db:personname>Steve Ball</db:personname>
        <db:email>Steve.Ball@health.gov.au</db:email>
        <db:affiliation>
          <db:orgname>Department of Health and Ageing</db:orgname>
          <db:orgdiv>PharmBiz</db:orgdiv>
        </db:affiliation>
      </db:author>

      <db:revhistory>
        <db:revision>
          <db:revnumber>2.0</db:revnumber>
          <db:date>2013-04-19</db:date>
          <db:authorinitials>SRB</db:authorinitials>
          <db:revremark>Redesigned pipeline.</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>1.0.1</db:revnumber>
          <db:date>2012-04-26</db:date>
          <db:authorinitials>SRB</db:authorinitials>
          <db:revremark>Use abbreviated QNames instead of URIs.</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>1.0</db:revnumber>
          <db:date>2011-12-19</db:date>
          <db:authorinitials>SRB</db:authorinitials>
          <db:revremark>Initial version.</db:revremark>
        </db:revision>
      </db:revhistory>
    </db:info>

    <db:para>This XSL stylesheet prepares pricing data for the calculation of the most cost efficient vial combination.</db:para>
    <db:para>The source document is the result of the vial-combo-puomp.xsl stylesheet.</db:para>
    <db:para>The result document is a modified PBS XML description of the listing.</db:para>
    <db:note>
      <db:para>In the stylesheet comments, PUOMP is short for "Per Unit-Of-Measure Price".</db:para>
    </db:note>
  </db:article>

  <xsl:output method="xml" indent='yes'/>
  <xsl:strip-space elements='*'/>

  <xsl:key name='puomp'
    match='pbs:infusible//pbs:tpp-reference'
    use='format-number(../pbs:tpuu-pharmacy-price/pbs:price/pbs:amount div ../pbs:vial-content, "0.00000000")'/>

  <xsl:key name='puomp-by-content'
    match='pbs:infusible//pbs:tpp-reference'
    use='concat(format-number(../pbs:tpuu-pharmacy-price/pbs:price/pbs:amount div ../pbs:vial-content, "0.00000000"), "x", ../pbs:vial-content)'/>

  <xsl:template match='pbs:reimbursement |
                       pbs:manufacturer'>
    <xsl:copy>
      <xsl:apply-templates select='@*'/>

      <xsl:apply-templates select='.//pbs:tpp-reference[count(.|key("puomp", format-number(../pbs:tpuu-pharmacy-price/pbs:price/pbs:amount div ../pbs:vial-content, "0.00000000"))[generate-id(ancestor::pbs:reimbursement|ancestor::pbs:manufacturer) = generate-id(current())][1]) = 1]'
        mode='puomp-group'>
        <xsl:sort select='format-number(../pbs:tpuu-pharmacy-price/pbs:price/pbs:amount div ../pbs:vial-content, "0.00000000")' order='ascending'/>
        <xsl:with-param name='parent' select='.'/>
      </xsl:apply-templates>
    </xsl:copy>
  </xsl:template>

  <xsl:template match='pbs:tpp-reference'
    mode='puomp-group'>
    <xsl:param name='parent' select='/..'/>

    <xsl:message></xsl:message>

    <xsl:variable name='puomp'
      select='format-number(../pbs:tpuu-pharmacy-price/pbs:price/pbs:amount div ../pbs:vial-content, "0.00000000")'/>

    <pbs:prices puomp='{$puomp}'>
      <xsl:apply-templates select='key("puomp", $puomp)[count(.|key("puomp-by-content", concat($puomp, "x", ../pbs:vial-content))[generate-id(ancestor::*[self::pbs:reimbursement|self::pbs:manufacturer]) = generate-id($parent)][1]) = 1]'
        mode='content-group'>
        <xsl:sort select='../pbs:vial-content' order='descending'/>
        <xsl:with-param name='parent' select='$parent'/>
      </xsl:apply-templates>
    </pbs:prices>
  </xsl:template>

  <xsl:template match='pbs:tpp-reference'
    mode='content-group'>
    <xsl:param name='parent' select='/..'/>

    <pbs:tpp-list>
      <xsl:copy-of select='../pbs:tpuu-ex-manufacturer'/>
      <xsl:copy-of select='../pbs:tpuu-pharmacy-price'/>
      <xsl:copy-of select='../pbs:vial-content'/>

      <xsl:copy-of select='key("puomp-by-content", concat(format-number(../pbs:tpuu-pharmacy-price/pbs:price/pbs:amount div ../pbs:vial-content, "0.00000000"), "x", ../pbs:vial-content))[generate-id(ancestor::*[self::pbs:reimbursement|self::pbs:manufacturer]) = generate-id($parent)]'/>
    </pbs:tpp-list>
  </xsl:template>

  <xsl:template name='copy' match="*|@*">
    <xsl:copy>
      <xsl:apply-templates select='@*|node()'/>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>
